package com.bank.ui.admin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import com.bank.dao.DepositTypeDAO;
import com.bank.entity.DepositType;

public class EditDepositTypeDialog extends JDialog {
    private final int savingID;
    private JTextField savingNameField;
    private JTextArea descriptionArea;

    public EditDepositTypeDialog(int savingID) {
        this.savingID = savingID;
        setTitle("修改存款业务");
        setSize(400, 300);
        setModal(true);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout(10, 10));

        // 主面板
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(8, 8, 8, 8);
        gbc.anchor = GridBagConstraints.WEST;

        // 业务类型编号（不可编辑）
        gbc.gridx = 0;
        gbc.gridy = 0;
        mainPanel.add(new JLabel("业务类型编号:"), gbc);

        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        JTextField savingIDField = new JTextField(String.valueOf(savingID), 15);
        savingIDField.setEditable(false);
        savingIDField.setBackground(new Color(240, 240, 240));
        mainPanel.add(savingIDField, gbc);

        // 业务名称
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        mainPanel.add(new JLabel("业务名称:"), gbc);

        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        savingNameField = new JTextField(15);
        mainPanel.add(savingNameField, gbc);

        // 描述
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE;
        mainPanel.add(new JLabel("描述:"), gbc);

        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.BOTH;
        descriptionArea = new JTextArea(4, 20);
        descriptionArea.setLineWrap(true);
        descriptionArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(descriptionArea);
        mainPanel.add(scrollPane, gbc);

        // 从数据库加载数据
        loadDepositTypeData();

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        JButton saveButton = new JButton("保存");
        saveButton.addActionListener(this::saveDepositType);

        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(e -> dispose());

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        add(mainPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void loadDepositTypeData() {
        try {
            DepositTypeDAO depositTypeDAO = new DepositTypeDAO();
            DepositType depositType = depositTypeDAO.getDepositTypeByID(savingID);

            if (depositType != null) {
                savingNameField.setText(depositType.getSavingName());
                descriptionArea.setText(depositType.getDescript());
            } else {
                JOptionPane.showMessageDialog(this,
                        "找不到指定的存款业务",
                        "错误",
                        JOptionPane.ERROR_MESSAGE);
                dispose();
            }
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    "获取存款业务信息失败: " + e.getMessage(),
                    "错误",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    private void saveDepositType(ActionEvent e) {
        String savingName = savingNameField.getText().trim();
        String description = descriptionArea.getText().trim();

        if (savingName.isEmpty()) {
            JOptionPane.showMessageDialog(this,
                    "业务名称不能为空",
                    "输入错误",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
            DepositType depositType = new DepositType(savingID, savingName, description);
            DepositTypeDAO depositTypeDAO = new DepositTypeDAO();
            depositTypeDAO.updateDepositType(depositType);

            JOptionPane.showMessageDialog(this,
                    "存款业务修改成功",
                    "成功",
                    JOptionPane.INFORMATION_MESSAGE);
            dispose();
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    "存款业务修改失败: " + ex.getMessage(),
                    "错误",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}